package com.jy.novel.dao;

import com.jy.novel.common.JdbcHelper;
import com.jy.novel.common.Page;
import com.jy.novel.pojo.NovelChapter;
import com.jy.novel.pojo.form.NovelChapterAddForm;
import com.jy.novel.pojo.form.NovelChapterEditForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jihb on 4/28/16.
 */
@Repository
public class NovelChapterDao {

    @Autowired
    JdbcHelper jdbcHelper;

    /**
     * 查询小说章节列表 分页
     * @param curPage
     * @param pageSize
     * @return
     */
    public Page<NovelChapter> findNovelChapterPage(long novelId,int curPage,int pageSize){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,name,novelId,content,chapternum from t_novel_chapter where novelId=? ");
        return jdbcHelper.queryForPageBean(sb.toString(),new Object[]{novelId},NovelChapter.class,curPage,pageSize);
    }

    /**
     *  查询小说章节列表 分页
     * @param novelId
     * @param curPage
     * @param pageSize
     * @return
     */
    public List<NovelChapter> findNovelChapterList(long novelId,int curPage,int pageSize){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,name,novelId,content,chapternum from t_novel_chapter where novelId=? ");
        return jdbcHelper.queryForListBean(sb.toString(),new Object[]{novelId},NovelChapter.class,curPage,pageSize);
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    public NovelChapter findNovelChapter(long id){
        return jdbcHelper.queryForBean("select id,name,novelId,content,chapternum from t_novel_chapter where where id=?",new Object[]{id},NovelChapter.class);
    }

    /**
     * 添加小说章节
     * @param addForm
     * @return
     */
    public boolean addNovelChapter(NovelChapterAddForm addForm){
        StringBuilder sb = new StringBuilder();
        sb.append("insert into t_novel_chapter(name,novelId,content,chapternum) values(?,?,?,?)");
        return jdbcHelper.update(sb.toString(),new Object[]{addForm.getName(),addForm.getNovelId(),addForm.getContent(),addForm.getChapternum()})>0;
    }

    /**
     * 编辑小说章节
     * @param editForm
     * @param id
     * @return
     */
    public boolean updateNovelChapter(NovelChapterEditForm editForm,long id){
        StringBuilder sb = new StringBuilder();
        sb.append("update t_novel_chapter set id=id");
        List params = new ArrayList();
        if (editForm.getName() != null){
            sb.append(",name=?");
            params.add(editForm.getName());
        }
        if (editForm.getContent() != null){
            sb.append(",content=?");
            params.add(editForm.getContent());
        }
        if (editForm.getChapternum() != null){
            sb.append(",chapternum=?");
            params.add(editForm.getChapternum());
        }
        sb.append(" where id=?");
        params.add(id);
        return jdbcHelper.update(sb.toString(),params.toArray())>0;
    }

    /**
     * 删除章节
     * @param id
     * @return
     */
    public boolean deleteNovelChapter(long id){
        return jdbcHelper.update("delete from t_novel_chapter where id=?",new Object[]{id})>0;
    }

}
